<?php
session_start();
require_once("../../connection.php");
require_once("../../../functions_shared.php");
require_once('postageapp_class.inc');

/* This php file should never be passed the parameter $_POST['user']
 * because it is being used as a condition in the php file fetcH_stream.php whic is included here
 */

$id = $_POST['id'];
$type1 = $_POST['type'];

//echo $id." ".$type1;

$arr1 = explode("_", $type1);
$type = $arr1[0];

//echo $type1.' '.$type.' '.$id;


if($type == "group")
{
	$res = mysql_query("SELECT * FROM site_posts WHERE `id`='$id'");
	if(mysql_num_rows($res)!=0)
	{
		$group_name_id = mysql_result($res,0,'type');
		$postas =  mysql_result($res,0,'postas');
		$text =  mysql_result($res,0,'text');
		$media = mysql_result($res,0,'media');
		
		//group name
		$arr = explode("_",$group_name_id);
		$gpid = $arr[1];
		$res1 = mysql_query("SELECT `name` FROM site_groups WHERE `id`='$gpid'");
		$gpname = mysql_result($res1,0,'name');
		
		if($gpname == "")
			$gpname = "your circle";
		
		// postas
		if($postas != "anonymous")
		{		
			$arr = explode("_",$postas);
			$postas = $arr[1];			
		}
		
		// media
		$arr = explode("_",$media);
		$media_type = $arr[0];
		if($media_type == "img")
			$medi_text = 'Image: <br><img src="'.$arr[1].'" style="width:100px" />';
		else if($media_type == "vid")
			$medi_text = 'Video: <a href="http://www.youtube.com/watch?v='.substr($media, 4).'" target="_blank">http://www.youtube.com/watch?v='.substr($media, 4).'</a>';
		else 
			$medi_text = "";
		
		$output = '<div style="font-family:calibri">
		<div style="color:#666"><strong>'.$postas.'</strong> posted in <a href="'.$site_url.'?cat=group&gpid='.$gpid.'&action=gausipp"><strong>'.$gpname.'</strong></a></div>
		<div style="font-size:12pt">'.convertUrl($text).'</div>
		<div>'.$medi_text.'</div><br>
		<div style="font-size:8pt">If you don\'t wish to receive these mails. You can change your settings <a href="'.$site_url.'?cat=settings" target="_blank">here</a>.</div>
		</div>';
		
		// getting the email id of the subscribers		// getting the email of all the members of the group				
		$result = mysql_query("SELECT * FROM site_group_members WHERE `gpid`='$gpid' AND `del`!='1'");		//$result = mysql_query("SELECT * FROM site_subscribe WHERE `gpid`='$gpid' AND `del`!='1'");
						
		if(!$result) echo mysql_error();
		else {
			while($row = mysql_fetch_array($result)){
				$fbid = $row['fbid'];
				if(mysql_num_rows(mysql_query("SELECT * FROM site_subscribe WHERE `gpid`='$gpid' AND `fbid`='$fbid' AND `del`='1'")) != 0)
						continue;
				// extracting the email id. if the field does not exist then continue				
				$temp = mysql_query("SELECT email FROM site_user_meta WHERE `fbid`='$fbid'");				
				if(mysql_num_rows($temp) == 0) continue;								
					  $email = mysql_result($temp,0,'email');				
				// add the user to mailing list only when the user was offline
				// the condition that the poster is the same as the logged in user is not required coz
				// the poster has to be online when he's posting this. and hence not requried
				if(is_user_online($fbid,"boolean") == 0)
					$mailers[] = $email;
			}	 
		}
		
		
	}
	else
		{
		}		
}

//echo json_encode($mailers);
if(sizeof($mailers) == 0)
 die();

 $to = "";
for($i=0;$i<sizeof($mailers);$i++)
{
	$to = $to.$mailers[$i];
	if($i != sizeof($mailers)-1)
	$to = $to.', ';
}

 // The subject of the message
 $subject = 'Postage App test email';

 // Setup some headers
 $header = array(
    'From'      => 'no_reply@gausipp.com',
    'Reply-to'  => 'mailroopam@gmail.com'
 );

 // The body of the message
 $mail_body = array(
    'text/plain' => 'You have new activity in your circle',
    'text/html' => $output
 );

 // Send it all
 PostageApp::mail($to, $subject, $mail_body, $header);
 
?>
